package com.guaika.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.common.StringChars;
import com.guaika.reggie.dto.SetmealDto;
import com.guaika.reggie.entity.Category;
import com.guaika.reggie.entity.Setmeal;
import com.guaika.reggie.entity.SetmealDish;
import com.guaika.reggie.service.CategoryService;
import com.guaika.reggie.service.SetmealDishService;
import com.guaika.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author GuaiKa
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /***
     * 保存套餐与菜品套餐关系
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public ResultMessage<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWidthSetmeal(setmealDto);
        return ResultMessage.success(StringChars.SAVE_MESSAGE.getStr());
    }

    /***
     * 返回套餐分页信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#page+'_'+#pageSize+'_'+#name",unless = "#result==null")
    @GetMapping("/page")
    public ResultMessage<Page> page(Integer page,Integer pageSize,String name){
        return setmealService.setmealPage(page, pageSize, name);
    }


    /***
     * 根据分类查询套餐列表
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.id+'_'+#setmeal.status",unless = "#result==null")
    @GetMapping("list")
    public ResultMessage<List<Setmeal>> list(Setmeal setmeal){
        return setmealService.setmealList(setmeal);
    }

    /***
     * 根据id删除套餐信息
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public ResultMessage<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWidthDish(ids);
        return ResultMessage.success(StringChars.DELETE_SUCCESS.getStr());
    }
}
